const express = require("express");
const { init } = require("./init");
const bodyParser = require("body-parser");
const jwt = require("jsonwebtoken");
const api = require("./routes");
const path = require('path');

const app = express();

//设置跨域访问
app.all("*", function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "*"); //Content-Type,Access-Token
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  // res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

//初始化数据库
init();

/* 以application/json格式解析数据 */
app.use(bodyParser.json());
/* 以application/x-www-form-urlencoded格式解析数据 */
app.use(bodyParser.urlencoded({ extended: false }));


/* 设置静态资源目录 */
app.use('/serverImage', express.static(path.join(__dirname, 'serverImage')));

app.get("/", (req, res) => res.send("Welcome Back"));
app.use("/api", api);

app.listen(5000, () => {
  console.log("启动服务，监听5000端口啦");
});
